package hxy.bytecode.file;

import java.io.*;

/**
 * @author eric
 * @description:
 * @date 12/9/19 10:31 AM
 */
public class GeneralFileOperate {

    public static void main(String[] args) throws FileNotFoundException {
        String path0 = "";
        String path1 = "";

        File fromFile = new File(path0);
        File toFile = new File(path1);
        new GeneralFileOperate().fileCopyNormal(fromFile, toFile);

    }

    /**
     * 普通的文件复制方法
     *
     * @param fromFile 源文件
     * @param toFile   目标文件
     * @throws FileNotFoundException 未找到文件异常
     */
    public void fileCopyNormal(File fromFile, File toFile) throws FileNotFoundException {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(fromFile));
            outputStream = new BufferedOutputStream(new FileOutputStream(toFile));
            byte[] bytes = new byte[1024];
            int i;
            //读取到输入流数据，然后写入到输出流中去，实现复制.
            // 先从输入流读取到缓冲区，再从缓冲区写入到文件输出流，缓冲区的目的就是为了解决两个输入与输出不协调的问题。
            while ((i = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

//        原文链接：https://blog.csdn.net/qq_16628781/article/details/70532307
    }
}
